Restructure modules and extract out SQL handling