refactored code for #28