Turned into gem, small modifications from original, fixed #1