migrated code to model, some other aesthetic changes