Cleaned up code, now fully functional