improving readability and performance

updated version of PR #225 against master branch

I'm looking forward to discuss about merging this PR :)