Fix signal handling and add comments