Cleaned up code somewhat