Basic logic done. TODO: fix sorting