rewrote code to prevent gcc warnings