Cleaned some code, added some comments, moved the signal to main function