Reorder code for better performance