Improved performance. Removed as many database calls as possible using select.