refactored code a little after reading a blog post