PouyanA - Added the database stuff and refactored the funk out of the code