Cleaned up code, added more comments