code refactored a little