minor refactor and bug fixes