Move object detection code into seperate subroutine