Add error handling and fix selection bug