Cleaning up control and making more general functions