refactored from sams comments