Cleaned the messy code a bit