Refactored code for better performance. Cleaned up variables