Fixed buggy context check by using the correct jquery object.
Merged duplicate actions checks into a function.