working version without mainloop