First working version
Error handling added