cleaned up slightly the code