further refatoring and cleaning up