fix based on mgorny's and tampakrap's suggestions