Combined all functionality into a single function