Slightly cleaned up logic