More functions, more comments, more logic