Tests and fix to allow return to work correctly within if statements. Closes #1297.