Cleaned up code, added some new features