Refactor and fix shit tons of bugs in groups.php