#488 refactored previous fix (still needs more refactoring)