Separate out background & frame detection code