Migrated code from closed-source project and cleaned up a bit