Almost done with first task, but in some cases functions doesn't work properly.