Fixed a bunch of bugs. Now accepted by Kattis.