Fixed mistakes in some sql queries, added paginating (kinda)