Clean up style and slightly improve logic