Added while-loops and exception handling