Some improvements. No more need to specify states count. No more useless arrays, now linked lists.