rough draft(different error cases are not handles) but will work for correct inputs