signal handling added; loops added where necessary