Initial implementation of build_query function

Still needs better integration with other functions