Fixed PAM bug and cleaned up code