Cleaned up/reorganized the code