Cleaned up and debugged