Formatting and logic cleanup