Solves the first task. Maybe we can optimize it?