critical bug fix
minor refactoring