Reorganized code, added error handling, and enhanced styles