Fix warnings and simplify/reorder the code