refactor complete, ready to add interrupt