incorporated changes from @dominikgrygiel