Cleaned up and mostly functional