Added mainloop and signal handlers