Fixed: Code cleanup and some minor bug fixes