Add specs, improve algorithm, uglify code