Cleaned up code based on Vinoth's review