Cleaned up code. Improved efficiency