Cleaned up the code and fixed magic numbers