refactoring and added image detection