Refeactor code for PR #160