Refactor security groups

Several refactors in security groups. I will do more refactoring but I'd like to get this merged already.