Cleaned up code based on Shaun's suggestions