Final code with all requriements