Modified code to now seperate the main body of the work for each boundary to a separate function call.