Refactored code for better organization and other improvements.