Almost tested, working solution