Added selection and fixed some bugs