srs: Refactor code, check for NULL pointers and prevent memory leaks