Improvements based on @rmascarenhas's suggestions