Cleaned up the code. Ready for merge