Issue 417

Simple fix for this issue. Reduced some duplicate code at the same time.