Cleaned up code, much easier to read now